package com.bnyy.medicalHousekeeper.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.adapter.GoodsAdapter;
import com.bnyy.medicalHousekeeper.base.RefreshFragment;
import com.bnyy.medicalHousekeeper.bean.Goods;
import com.bnyy.medicalHousekeeper.bean.IndexData;
import com.bnyy.medicalHousekeeper.constants.GoodsEditType;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.function.Consumer;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/6
 **/
public class GoodsFragment extends RefreshFragment {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private int goodsType;
    private String keyword;
    private GoodsEditType goodsEditType;
    private GoodsAdapter adapter;
    private int page = 1;
    private IndexData.Data.Type type;

    @Override
    public OnRefreshListener registerOnRefreshListener() {
        return new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getGoodsList();
            }
        };
    }

    @Override
    public OnLoadMoreListener registerOnLoadMoreListener() {
        return new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getGoodsList();
            }
        };
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_recycler_view;
    }

    public static GoodsFragment getInstance(IndexData.Data.Type type, int goodsType, GoodsEditType goodsEditType) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("type", type);
        bundle.putInt("goodsType", goodsType);
        bundle.putSerializable("goodsEditType", goodsEditType);
        GoodsFragment fragment = new GoodsFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        Bundle bundle = getArguments();
        if (bundle != null) {
            type = (IndexData.Data.Type) bundle.getSerializable("type");
            goodsType = bundle.getInt("goodsType", 1);
            goodsEditType = (GoodsEditType) bundle.getSerializable("goodsEditType");
            adapter = new GoodsAdapter(mContext, goodsType, goodsEditType);
            recyclerView.setAdapter(adapter);
        }

        if (mContext instanceof GoodsAdapter.OnCheckedChangeListener) {
            ((GoodsAdapter.OnCheckedChangeListener) mContext).onGoodsEditTypeChange(new Consumer<GoodsEditType>() {
                @Override
                public void accept(GoodsEditType goodsEditType) {

//                    switch (GoodsFragment.this.goodsEditType) {
//                        case NORMAL:
//                            mRefreshLayout.autoRefresh();
//                            break;
//                        case ADD:
//                            mRefreshLayout.autoRefresh();
//                            break;
//                        case REMOVE:
//                            break;
//                    }

                    GoodsFragment.this.goodsEditType = goodsEditType;

                    mRefreshLayout.autoRefresh();
                }
            });
        }

        mRefreshLayout.autoRefresh();
    }

    public void afterEdit(HashSet<Integer> goodsIds) {
        ArrayList<Goods> datas = adapter.getDatas();
        for (Goods goods : datas) {
            if (goodsIds.contains(goods.getGoods_id())) {
                mRefreshLayout.autoRefresh();
                break;
            }
        }
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
        mRefreshLayout.autoRefresh();
    }

    private void getGoodsList() {

        HashMap<String, Object> params = new HashMap<>();
        params.put("page_index", page);
        params.put("page_size", 20);
        params.put("type", goodsType);
        if (!TextUtils.isEmpty(keyword)) {
            params.put("keyword", keyword);
        }

        BaseObserverImpl<ArrayList<Goods>> observer = new BaseObserverImpl<ArrayList<Goods>>(mRefreshLayout) {
            @Override
            public void onSuccess(ArrayList<Goods> goods) {
                super.onSuccess(goods);

                if (page == 1) {
                    llNoData.setVisibility(goods.size() > 0 ? View.GONE : View.VISIBLE);
                    adapter.refresh(goods);
                } else {
                    adapter.loadMore(goods);
                }
                mRefreshLayout.setEnableLoadMore(goods.size() >= 20);
            }
        };

        switch (type.getType()) {
            case 2:
                switch (goodsEditType) {
                    case NORMAL:
                    case REMOVE:
                        requestManager.request(requestManager.mJavaRetrofitService.getRelationGoodsList(RequestManager.getJsonRequestBody(params)), observer);
                        break;
                    case ADD:
                        requestManager.request(requestManager.mJavaRetrofitService.getNoRelationGoodsList(RequestManager.getJsonRequestBody(params)), observer);
                        break;
                }
                break;
            case 3:
                requestManager.request(requestManager.mJavaRetrofitService.getNewProducts(RequestManager.getJsonRequestBody(params)), observer);
                break;
        }
    }
}



























































